/**
 * 
 */
package mx.netbrain.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import mx.netbrain.converter.PropertiesUserConverter;
import mx.netbrain.dao.PropertiesUserDao;
import mx.netbrain.dao.impl.PropertiesUserDaoImpl;
import mx.netbrain.model.ObjectNetbrain;
import mx.netbrain.model.PropertiesUser;
import mx.netbrain.model.Status;
import mx.netbrain.model.TypeField;

/**
 * @author julian
 * 
 */
public class PropertiesUserBean {

	private String label;
	private List<TypeField> listType;
	private List<PropertiesUser> listProperties;
	private TypeField typeField;
	private Integer size;
	private Boolean mandatory;
	private Boolean active;
	private PropertiesUser propertiesUser;
	private Status status = new Status();
	private ObjectNetbrain object = new ObjectNetbrain();

	public PropertiesUserBean() {
		listType = PropertiesUserConverter.typesDB;
	}

	public void prepareProperties(Long id) {
		PropertiesUserDao propertiesUserDao = new PropertiesUserDaoImpl();
		propertiesUser = propertiesUserDao.findById(id);
		active = propertiesUser.getStatus().isValor();
		typeField = propertiesUser.getTypeField();
	}

	public void save() {
		PropertiesUserDao propertiesUserDao = new PropertiesUserDaoImpl();
		propertiesUser = new PropertiesUser();
		propertiesUser.setLabel(label);
		propertiesUser.setTypeField(typeField);
		propertiesUser.setSize(size);
		propertiesUser.setMandatory(mandatory);
		if (active) {
			status.setId(1);
		} else {
			status.setId(2);
		}
		propertiesUser.setStatus(status);
		object.setId(Long.parseLong("1"));
		propertiesUser.setObjectNetbrain(object);
		propertiesUser.setRegistry(new Date());
		propertiesUser.setName("_" + label);
		propertiesUserDao.save(propertiesUser);
		propertiesUser = new PropertiesUser();
		status = new Status();
		label = new String();
		typeField = new TypeField();
		size = null;
		mandatory = null;
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(
				"Datos actualizados correctamente!", "Actualizado"));
	}

	public void edit() {
		PropertiesUserDao propertiesUserDao = new PropertiesUserDaoImpl();
		if (active) {
			status.setId(1);
		} else {
			status.setId(2);
		}
		propertiesUser.setStatus(status);
		propertiesUser.setTypeField(typeField);
		propertiesUserDao.update(propertiesUser);
		propertiesUser = new PropertiesUser();
		status = new Status();
		typeField = new TypeField();
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public List<PropertiesUser> getListProperties() {
		PropertiesUserDao propertiesUserDao = new PropertiesUserDaoImpl();
		listProperties = propertiesUserDao.listAll();
		return listProperties;
	}

	public void setListProperties(List<PropertiesUser> listProperties) {
		this.listProperties = listProperties;
	}

	public TypeField getTypeField() {
		return typeField;
	}

	public void setTypeField(TypeField typeField) {
		this.typeField = typeField;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Boolean getMandatory() {
		return mandatory;
	}

	public void setMandatory(Boolean mandatory) {
		this.mandatory = mandatory;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public List<TypeField> completeType(String query) {
		List<TypeField> suggestions = new ArrayList<TypeField>();
		for (TypeField p : listType) {
			if (p.getDescription().startsWith(query))
				suggestions.add(p);
		}
		return suggestions;
	}

	public PropertiesUser getPropertiesUser() {
		if (propertiesUser == null) {
			propertiesUser = new PropertiesUser();
		}
		return propertiesUser;
	}

	public void setPropertiesUser(PropertiesUser propertiesUser) {
		this.propertiesUser = propertiesUser;
	}

}
